/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.utils;

import java.util.*;
import java.sql.ResultSet;
import java.sql.SQLException;

public class VectorVectorString {
	private Vector store=new Vector();

	public VectorVectorString() {}

	public VectorString get(int pos){
		return (VectorString)store.get(pos);
	}

	public VectorVectorString(ResultSet rs,String cols[])
	throws SQLException {
		for (;rs.next();) {
			VectorString vs = new VectorString();
			for (int i =0;i < cols.length;i++) {
				System.out.println("Adding " + rs.getString(cols[i])); 
				vs.add(rs.getString(cols[i]));
			}
			add(vs);
		}
	}

	public VectorVectorString(ResultSet rs,int cols[])
	throws SQLException {
		for (;rs.next();) {
			VectorString vs = new VectorString();
			for (int i =0;i < cols.length;i++) {
				vs.add(rs.getString(cols[i]));
			}
			add(vs);
		}
	}
	
	// Returns a VectorString from all the strings at position col 
	public VectorString substring(int col) {
		VectorString vs = new VectorString(); 
		for (int i = 0;i < size();i++) {
			vs.add(get(i).get(col));
		}
		return vs;
	}
	
	public VectorString getUniqueByColumn(int col) {
		VectorString vs = new VectorString();
		for (int i = 0;i < size();i++) {
			if (!vs.contains(get(i).get(col))) {
				vs.add(get(i).get(col));
			}
		}
		return vs;
	}

	public void replaceAll(String find_str,String replace,int col) {
		for (int i = 0;i < size();i++) {
			get(i).replaceAll(find_str,replace,col);
		}
	}
	
	// do a bubble sort
	public void sortByColumn(int col) {
		for (int i = 0;i < size();i++) {
			for (int e = 0;e < size();e++) {
				if ((i > e && get(i).get(col).compareTo(get(e).get(col)) < 0) ||
					(i < e && get(i).get(col).compareTo(get(e).get(col)) > 0)) {
					VectorString vvs = get(i);
					set(i,get(e));
					set(e,vvs);
				}
			}
		}
	}

	public VectorVectorVectorString getGroupByColumn(int col) {
		VectorVectorVectorString vvvs = new VectorVectorVectorString();
		VectorString unique = getUniqueByColumn(col);
		for (int i = 0;i < unique.size();i++) {
			VectorVectorString vvs = new VectorVectorString();
			for (int e = 0;e < size();e++) {
				if (unique.get(i).equals(get(e).get(col))) {
					vvs.add(get(e));
				}
			}
			vvvs.add(vvs);
		}
		return vvvs; 
	}

	public void add(VectorString item){
		store.add(item);
	}

	public void add(VectorVectorString vvs) {
		for (int i = 0; i < vvs.size();i++) {
			add(vvs.get(i));
		}
	}
	
	public void set(int index,VectorString item) {
		store.set(index,item);
	}
	
	public boolean remove(VectorString item){
		return store.remove(item);
	}

	public VectorString remove(int pos){
		return (VectorString) store.remove(pos);
	}

	public void clear() {
		store.clear();
	}

	public int size(){
		return store.size();
	}
	
	public int indexOf(String str,int col) {
		for (int i =0;i < size();i++) {
			if (get(i).get(col).equals(str)) {
				return i;
			}
		}
		return -1;
	}
	
	public VectorVectorString minus(VectorVectorString exclude,int col) {
		VectorVectorString vvs = new VectorVectorString();
		for (int i = 0;i < size();i++) {
			if (!exclude.contains(get(i).get(col),col)) {
				vvs.add(get(i));
			}
		}
		return vvs;
	}
	
	public boolean contains(String str,int col) {
		return indexOf(str,col) != -1;
	}
	
	public static class VectorVectorVectorString {
		private Vector store=new Vector();

		public VectorVectorVectorString() {}

		public void sortByColumn(int col) {
			for (int i =0;i < size();i++) {
				get(i).sortByColumn(col);
			}
		}
		
		public void replaceAll(String find_str,String replace,int col) {
			for (int i = 0;i < size();i++) {
				get(i).replaceAll(find_str,replace,col);
			}
		}
	
		public VectorVectorString get(int pos){
			return (VectorVectorString)store.get(pos);
		}

		public void add(VectorVectorString item){
			store.add(item);
		}

		public boolean remove(VectorVectorString item){
			return store.remove(item);
		}

		public VectorVectorString remove(int pos){
			return (VectorVectorString) store.remove(pos);
		}

		public void clear() {
			store.clear();
		}

		public int size(){
			return store.size();
		}

	}

}

